# coding=utf-8
"模型类，主要封装数据库操作"

import web
import datetime

#数据库类
db = web.database(dbn="mysql", db="test", user = "root", pw = "")
#读取所有博客
def get_posts():
    return db.select('entries', order = 'id DESC')

#获取博客内容
def get_post(id):
    try:
        return db.select('entries', where = 'id=$id', vars = locals())[0]
    except IndexError:
        return None

#新建博客
def new_post(title, text):
    db.insert('entries',
             title = title,
             content = text,
             posted_on = datetime.datetime.utcnow())

#删除博客
def del_post(id):
    db.delete('entries', where = 'id = $id', vars = locals())

#修改博客
def update_post(id, title, text):
    db.update('entries',
             where = 'id = $id',
             vars = locals(),
             title = title,
             content = text)
